package cn.touna.jss.modules.third.youfen;

import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.contraints.facet.Second;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.properties.disconf.YoufenProperties;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.youfen.entity.YoufenRequestEntity;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.http.HttpsUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * \* User: MeiZhongHao
 * \* Date: 2018-05-04
 * \* Time: 15:08
 * \* Description:
 * \
 */
@Service
public class YoufenService {

    private final static Logger logger = LogManager.getLogger(YoufenService.class);

    private final static String STATUS_CODE = "statusCode";

    private final static String RES_CODE = "resCode";

    private final static String DATA = "data";

    private final static String STATUS_SUCCESS_CODE = "2012";
    private final static String NO_DATA = "2007";

    private final static String RES_SUCCESS_CODE = "0000";

    @Autowired
    private YoufenProperties youfenProperties;

    @ApiMapping(value = "youfen.travelPortraits",desc = "优分.优分出乘画像查询")
    public Response queryTravelPortraits(YoufenRequestEntity youfenEntity) {
        Response response = new Response<>();
        try {
            //校验
            Response validResponse = ValidUtil.valid(youfenEntity, First.class);
            if (validResponse != null)
                return validResponse;
            try {
                //组装get请求参数
                Map<String,String> params = paramsAssemble(youfenEntity);
                String res = HttpsUtil.getInstance().get(youfenProperties.getUrl()+youfenProperties.getTravelPortraitsMethod()
                        ,params);
                JSONObject result = JSONObject.parseObject(res);
                if (result.getString(RES_CODE) != null &&
                        result.getString(RES_CODE).equals(RES_SUCCESS_CODE)) {
                    JSONObject data = JSONObject.parseObject(result.getString(DATA));
                    if (data.getString(STATUS_CODE) != null &&
                            data.getString(STATUS_CODE).equals(STATUS_SUCCESS_CODE)){
                        return ResponseFactory.success(result);
                    }
                }
                return ResponseFactory.fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_ERROR,res);
            }catch (Exception e){
                return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
            }

        } catch (Exception e) {
            logger.error("", e);
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
        }
    }


    @ApiMapping(value = "youfen.education",desc = "优分.学历查询")
    public Response education(YoufenRequestEntity youfenEntity) {
        Response response = new Response<>();
        try {
            //校验
            Response validResponse = ValidUtil.valid(youfenEntity, Second.class);
            if (validResponse != null)
                return validResponse;
            try {
                //组装get请求参数
                Map<String,String> params = new HashMap<>();
                params.put("idcard",youfenEntity.getIdCard());
                params.put("account",youfenProperties.getAccount());
                params.put("name",youfenEntity.getName());
                String url = youfenProperties.getUrl()+youfenProperties.getEducation();
                logger.info("youfen.education req url:{},params:{}",url,params);
                String res = HttpsUtil.getInstance().get(url ,params);
                logger.info("youfen.education result:{}",res);
                JSONObject result = JSONObject.parseObject(res);
                if (result.getString(RES_CODE) != null &&
                        result.getString(RES_CODE).equals(RES_SUCCESS_CODE)) {
                    JSONObject data = JSONObject.parseObject(result.getString(DATA));
                    if (data.getString(STATUS_CODE) != null &&   ( data.getString(STATUS_CODE).equals(STATUS_SUCCESS_CODE)|| data.getString(STATUS_CODE).equals(NO_DATA)  )){
                        return response.success(result);
                    }
                }
                return response.fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_ERROR,res);
            }catch (Exception e){
                return response.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
            }

        } catch (Exception e) {
            logger.error("", e);
            return response.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR,e.getMessage());
        }
    }





    private Map<String,String> paramsAssemble(YoufenRequestEntity youfenEntity){
        Map<String,String> map = new HashMap<>();
        if (!StringUtils.isEmpty(youfenEntity.getIdCard())){
            map.put("idcard",youfenEntity.getIdCard());
        }
        if (!StringUtils.isEmpty(youfenEntity.getCityLevel())){
            map.put("S010",youfenEntity.getCityLevel());
        }
        if (!StringUtils.isEmpty(youfenEntity.getSex())){
            map.put("S001",youfenEntity.getSex());
        }
        if (!StringUtils.isEmpty(youfenEntity.getTravelCity())){
            map.put("S007",youfenEntity.getTravelCity());
        }
        if (!StringUtils.isEmpty(youfenEntity.getUpor())){
            map.put("S006",youfenEntity.getUpor());
        }
        map.put("type",youfenProperties.getType());
        map.put("account",youfenProperties.getAccount());
        return map;
    }
}